// pages/user/components/NicknameDia.ts
import { doUpdInfo } from '~/api/user'
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false as boolean,
    nickName: '' as string
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async handSubmit(this: any) {
      if (!this.data.nickName) {
        wx.showToast({
          icon: 'none',
          title: '请输入昵称'
        })
        return
      }
      await doUpdInfo({
        nickName: this.data.nickName
      })
      wx.showToast({
        icon: 'none',
        title: '更改成功'
      })
      this.triggerEvent("success")
      this.close()
    },
    handChange(this: any, e: any) {
      this.setData({
        nickName: e.detail.value
      })
    },
    onClickNickname(this: any, e: any) {
      this.setData({
        nickName: e.detail.value
      })
    },
    open(this: any, nickName: string) {
      this.setData({
        visible: true,
        nickName,
      });
    },
    close(this: any) {
      this.setData({
        visible: false
      })
      let timer = setTimeout(() => {
        this.setData({
          nickName: ''
        })
        clearTimeout(timer)
      }, 500)
    },
    onVisibleChange(this: any, e: any) {
      if (!e.detail.visible) {
        this.close()
      }
    },
  }
})